package wifi_locator_client.just.wzy.wifi_locator_client;

import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 同步数据界面
 */

public class SyncActivity extends ActionBarActivity {

    private Button openWifi;
    private Button closeWifi;
    private Button scanWifi;
    private Button clearWifi;
    private Button syncWifi;
    private TextView wifiInfo;
    private EditText ipAddress;
    private EditText portNumber;

    private WifiController wifiController;
    private List<ScanResult> scanResults;
    private List<String> filtrateTable;

    private String host;
    private int port;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sync_layout);

        wifiController = new WifiController(SyncActivity.this);
        filtrateInit();
        init();
    }


    /**
     * 筛选器初始化，用于筛选初所需的热点名称
     */
    private void filtrateInit() {
        filtrateTable = new ArrayList<>();
        String str[] = {"ChinaUnicom", "CU_Campus", "CMCC-EDU", "ChinaNet-EDU", "CMCC-JDK"};
        for (String x : str) {
            filtrateTable.add(x);
        }
    }

    /**
     * 控件初始化
     */
    void init() {
        openWifi = (Button) findViewById(R.id.openWifi);
        closeWifi = (Button) findViewById(R.id.closeWifi);
        scanWifi = (Button) findViewById(R.id.scanWifi);
        clearWifi = (Button) findViewById(R.id.clearWifi);
        syncWifi = (Button) findViewById(R.id.syncWifi);
        wifiInfo = (TextView) findViewById(R.id.wifiInfo);
        ipAddress = (EditText) findViewById(R.id.ipAddress);
        portNumber = (EditText) findViewById(R.id.portNumber);
        openWifi.setOnClickListener(new MyListener());
        closeWifi.setOnClickListener(new MyListener());
        scanWifi.setOnClickListener(new MyListener());
        clearWifi.setOnClickListener(new MyListener());
        syncWifi.setOnClickListener(new MyListener());
    }

    /**
     * 筛选器
     * @param scanResults 扫描出的热点信息
     */
    private void filtrator(List<ScanResult> scanResults) {
        for (int i = 0; i < scanResults.size(); i++) {
            if (!filtrateTable.contains(scanResults.get(i).SSID)) {
                scanResults.remove(i);
                i--;
            }
        }
    }

    /**
     * 获取所有AP的相关信息
     */
    void getAllWifiList() {
        if (scanResults != null)
            scanResults.clear();
        scanResults = wifiController.scanWifi();
        filtrator(scanResults);
        StringBuffer sb = new StringBuffer();
        if (!scanResults.isEmpty())
            for (int i = 0; i < scanResults.size(); i++) {
                sb.append(scanResults.get(i).level + "   ").append(scanResults.get(i).SSID + "\n");
            }
        wifiInfo.setText(sb);
    }


    /**
     * 清除所有热点信息
     */
    void clearAllWifiList() {
        wifiInfo.setText(null);
    }

    /**
     * 内部类，控件监听
     */
    private class MyListener implements OnClickListener {

        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.openWifi:
                    wifiController.openWifi();
                    Toast.makeText(SyncActivity.this, "WIFI已打开！可以开始实验！", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.closeWifi:
                    wifiController.closeWifi();
                    Toast.makeText(SyncActivity.this, "WIFI已关闭！", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.scanWifi:
                    getAllWifiList();
                    Toast.makeText(SyncActivity.this, "RSSI获取成功!", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.clearWifi:
                    Toast.makeText(SyncActivity.this, "WIFI信息已清除", Toast.LENGTH_SHORT).show();
                    clearAllWifiList();
                    break;
                case R.id.syncWifi:
                    host = ipAddress.getText().toString();
                    port = Integer.parseInt(portNumber.getText().toString());
                    TransmissionManager transmissionManager = new TransmissionManager(host, port, SyncActivity.this, scanResults);
                    transmissionManager.sendMessage(0);
                    break;
                default:
                    break;
            }

        }
    }




}
